/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import kankan.wheel.widget.WheelView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.dpndeveloper.wtc.adapter.DayListAdapter;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.dataprovider.WtcIntervalDataBaseHelper;
import com.dpndeveloper.wtc.listener.OnDateChangedListener;
import com.dpndeveloper.wtc.util.TimeUtil;
import com.dpndeveloper.wtc.util.widget.DateWidget;

/**
 * <p>
 * Day History Activity: activity to show history of a week selecting a day
 * <b>Note: a week starts on Monday and ends on Sunday
 * <p>
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class WtcWeekHistoryActivity extends Activity implements
		OnDateChangedListener {

	/* Date Widget to select the week */
	private DateWidget					m_dateWidget;

	/* Instance of DatabaseHelper to manage WtcIntervals */
	private WtcIntervalDataBaseHelper	m_wtcIntervalDatabaseHelper;

	/* The interval of selected week */

	private List<WtcInterval>			m_weekIntervals;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_week_history);

		// Initialize wheels
		WheelView day = (WheelView) findViewById(R.id.historyDay);
		WheelView month = (WheelView) findViewById(R.id.historyMonth);
		WheelView year = (WheelView) findViewById(R.id.historyYear);

		m_dateWidget = new DateWidget(this, day, month, year);
		updateWidgets();
		m_dateWidget.addOnDateChangedListener(this);

		ListView list = (ListView) findViewById(R.id.listViewElement);
		registerForContextMenu(list);
	}

	@Override
	public void onDateChanged(DateWidget dateWidget) {
		// update content if it is selected a day
		updateWidgets();
	}

	@Override
	protected void onDestroy() {
		m_dateWidget.removeOnDateChangedListener(this);
		m_wtcIntervalDatabaseHelper.close();
		super.onDestroy();
	}

	/**
	 * Update the contents of Activity
	 */
	private void updateWidgets() {

		Calendar startDate = m_dateWidget.getSelectedDate();

		// If selected date is Sunday
		if (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			// set on previous Monday
			startDate.set(Calendar.DAY_OF_YEAR,
					startDate.get(Calendar.DAY_OF_YEAR) - 6);
		} else {
			startDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		}

		Calendar endDate = Calendar.getInstance(TimeZone.getDefault());
		endDate.setTime(startDate.getTime());
		// set on next Sunday
		endDate.add(Calendar.DAY_OF_YEAR, 6);
		endDate.set(Calendar.HOUR_OF_DAY, 23);
		endDate.set(Calendar.MINUTE, 59);
		endDate.set(Calendar.SECOND, 59);
		endDate.set(Calendar.MILLISECOND, 999);

		m_weekIntervals = getWtcIntervalDataBaseHelper().selectByInterval(
				startDate, endDate);

		String DATE_FORMAT = "dd/MM/yyyy";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		sdf.setTimeZone(TimeZone.getDefault());
		String startDayString = sdf.format(startDate.getTime());
		String endDayString = sdf.format(endDate.getTime());
		String weekDetailString = getResources().getString(R.string.weekFromTo,
				startDayString, endDayString);
		TextView weekDetail = (TextView) findViewById(R.id.textViewWeekDetail);
		weekDetail.setText(weekDetailString);

		TextView weekInfo = (TextView) findViewById(R.id.textViewWeekInfo);
		
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		if (endDate.compareTo(now)>0)
		{
			endDate = now;
		}
		weekInfo.setText(TimeUtil.getWorkTimeAsString(m_weekIntervals, endDate));

		ListView list = (ListView) findViewById(R.id.listViewElement);
		list.setAdapter(new DayListAdapter(this, startDate, m_weekIntervals));
	}

	/**
	 * Returns the instance of WtcIntervalDataBaseHelper
	 * 
	 * @return instance of WtcIntervalDataBaseHelper
	 */
	private WtcIntervalDataBaseHelper getWtcIntervalDataBaseHelper() {
		if (m_wtcIntervalDatabaseHelper == null) {
			m_wtcIntervalDatabaseHelper = new WtcIntervalDataBaseHelper(this);
		}
		return m_wtcIntervalDatabaseHelper;
	}

}
